package main

import (
	dc "gitee.com/DreamPublic/signal/pkg/impl/datachannel"
	ws "gitee.com/DreamPublic/signal/pkg/impl/websocket"
	"gitee.com/DreamPublic/signal/pkg/impl/webtrans"
	"gitee.com/DreamPublic/signal/pkg/log"
	"gitee.com/DreamPublic/signal/pkg/proto"
	"go.uber.org/zap"
	"os"
	"strconv"
	"time"
)

func main() {
	log.InitLog("main")
	webTransPort := 0
	webSocketPort := 0
	dataChannelPort := 0
	webrtcPort := 0
	if 5 == len(os.Args) {
		webTransPort, _ = strconv.Atoi(os.Args[1])
		webSocketPort, _ = strconv.Atoi(os.Args[2])
		dataChannelPort, _ = strconv.Atoi(os.Args[3])
		webrtcPort, _ = strconv.Atoi(os.Args[4])
	}
	go webTrans(webTransPort)
	go websocket(webSocketPort)
	go datachannel(dataChannelPort, webrtcPort)
	select {}
}

func webTrans(port int) {
	if 0 == port {
		port = 9990
	}
	// 此处certFile keyFile必填
	signal := webtrans.NewSignal(port, "key/server.cer", "key/server.key")

	signal.OnConnect(func(session proto.Session) {
		zap.L().Info("客户端连接")

		go func() {
			for true {
				now := time.Now()
				err := session.Emit("PING", strconv.FormatInt(now.UnixMilli(), 10))
				if nil != err {
					break
				}
				time.Sleep(time.Second)
			}
		}()

		//session.On("*",...) 代表监听所有事件
		session.On("PING", func(cbStruct proto.SessionCbStruct) {
			zap.L().Info("get data", zap.String("data", cbStruct.Msg))
			_ = session.Emit("PONG", cbStruct.Msg)
		})
	})

	signal.OnClose(func(session proto.Session) {
		zap.L().Info("客户端断开")
	})

	signal.OnAuth(func(auth proto.AuthCbStruct) bool {
		return true
	})

	err := signal.Run()
	if nil != err {
		zap.L().Error("start fail", zap.Error(err))
	}
}

func websocket(port int) {
	if 0 == port {
		port = 9991
	}
	//此处certFile keyFile可为空
	signal := ws.NewSignal(port, "key/server.cer", "key/server.key")

	signal.OnConnect(func(session proto.Session) {
		zap.L().Info("客户端连接")

		go func() {
			for true {
				now := time.Now()
				err := session.Emit("PING", strconv.FormatInt(now.UnixMilli(), 10))
				if nil != err {
					break
				}
				time.Sleep(time.Second)
			}
		}()

		//session.On("*",...) 代表监听所有事件
		session.On("PING", func(cbStruct proto.SessionCbStruct) {
			zap.L().Info("get data", zap.String("data", cbStruct.Msg))
			_ = session.Emit("PONG", cbStruct.Msg)
		})
	})

	signal.OnClose(func(session proto.Session) {
		zap.L().Info("客户端断开")
	})

	signal.OnAuth(func(auth proto.AuthCbStruct) bool {
		return true
	})

	err := signal.Run()
	if nil != err {
		zap.L().Error("start fail", zap.Error(err))
	}
}

func datachannel(port, webrtcPort int) {
	if 0 == port {
		port = 9992
	}
	if 0 == webrtcPort {
		webrtcPort = 9993
	}
	//此处certFile keyFile可为空
	signal := dc.NewSignal(port, webrtcPort, "key/server.cer", "key/server.key")

	signal.OnConnect(func(session proto.Session) {
		zap.L().Info("客户端连接")

		go func() {
			for true {
				now := time.Now()
				err := session.Emit("PING", strconv.FormatInt(now.UnixMilli(), 10))
				if nil != err {
					break
				}
				time.Sleep(time.Second)
			}
		}()

		//session.On("*",...) 代表监听所有事件
		session.On("PING", func(cbStruct proto.SessionCbStruct) {
			zap.L().Info("get data", zap.String("data", cbStruct.Msg))
			_ = session.Emit("PONG", cbStruct.Msg)
		})
	})

	signal.OnClose(func(session proto.Session) {
		zap.L().Info("客户端断开")
	})

	signal.OnAuth(func(auth proto.AuthCbStruct) bool {
		return true
	})

	err := signal.Run()
	if nil != err {
		zap.L().Error("start fail", zap.Error(err))
	}
}
